#include "LightsWindow.hpp"
#include <fltk/events.h>
#include <stdio.h>
#include <fltk/ask.h>
#include <time.h>
#include <stdlib.h>
#include <fltk/ToggleButton.h>
#include <fltk/MultiLineOutput.h>
#include "Lights.hpp"

using namespace fltk;


Window *LightsWindow::win_help = 0;

LightsWindow::LightsWindow(int w, int h)
 : Window(w,h,"NYNO - Lights"),cell_size(40),
   _color_off(0xcccccc00),_color_on(0xffff0000) {
    begin();
    int btn_x = 16 + cell_size*5;
    int btn_y = 16;
   
    _game_id = new IntInput(btn_x+15,btn_y,40,25,"#:");
    _game_id->value(13);
    btn_y += 30;
    Button *btn_new     = new Button(btn_x,btn_y,70,30," New ");
    btn_new->callback(new_cbk);
    btn_y += 35;
    Button *btn_solve  = new Button(btn_x,btn_y,70,30," Solve ");
    btn_solve->callback(solve_cbk);
    btn_y += 35;
    Button *btn_reset  = new Button(btn_x,btn_y,70,30," Reset ");
    btn_reset->callback(reset_cbk);
    btn_y += 35;
    Button *btn_help  = new Button(btn_x,btn_y,70,30," Help ");
    btn_help->callback(help_cbk);

    drawLightsGrid();
    end();
}

void LightsWindow::drawLightsGrid(){
    int ox = 5;
    int oy = 10; 
    ToggleButton *cell=0;
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            int index = i*5+j;
            //if(light_cells[index]==0){    
                cell = new ToggleButton(ox+cell_size*j,oy+cell_size*i,cell_size,cell_size);
                light_cells[index] = cell;
                bool v = cell->value();
                cell->buttoncolor(v?_color_on:_color_off);
                cell->callback(LightsWindow::check_cbk,(void *)(i*5+j));
            //}
        }
    }
}

void LightsWindow::switch_single_light(int index) {
    if( index >= 0 && index<25){
        bool v = light_cells[index]->value();
        light_cells[index]->value(!v);
        light_cells[index]->buttoncolor(!v?_color_on:_color_off);
    }
}

void LightsWindow::switch_light(int index) {
    bool v;
    v = light_cells[index]->value();
    light_cells[index]->buttoncolor(v?_color_on:_color_off); 

    if( index%5 > 0)  switch_single_light(index-1);
    if( index%5 < 4)  switch_single_light(index+1);
    if( index-5 >=0)  switch_single_light(index-5);
    if( index+5 < 25) switch_single_light(index+5);  

    return;
}

void LightsWindow::check_game(int index) {
    light_cells[index]->label("");
    switch_light(index);
    
    for(int i=0;i<25;i++){
        bool v = light_cells[i]->value();
        if(!v) return;
    }

    message("Congradulations!!!\nProblem solved!");
    return;
}



void LightsWindow::new_game() {
    int seed = (int) _game_id->lvalue();
    
    for(int i=0;i<25;i++){
        light_cells[i]->value(false);
        light_cells[i]->label("");
        light_cells[i]->buttoncolor(false?_color_on:_color_off);
    }
    srand(seed);
    int cnt = 0;
    while(cnt++ < 25){
        int index = rand()%25;
        switch_light(index);
    }
    
    for(int i=0;i<25;i++){
        game[i] = light_cells[i]->value()?1:0;
    }
    
    return;
}


void LightsWindow::reset_game() {
    for(int i=0;i<25;i++){
        light_cells[i]->value(game[i]==1?true:false);
        light_cells[i]->label("");
        light_cells[i]->buttoncolor(game[i]==1?_color_on:_color_off);
        light_cells[i]->redraw();
    }
    
    return;
}

void LightsWindow::solve_game() {
    int seed = (int) _game_id->lvalue();
    int problem[25];
    for(int i=0;i<25;i++){
        problem[i] = light_cells[i]->value()?1:0;
    }
    
    Lights lights;
    lights.init();
    lights.set_problem(problem);
    lights.solve();
    const int *rv = lights.get_solution();
    
    for(int i=0;i<25;i++){
        light_cells[i]->label("");
        light_cells[i]->redraw();
        if(rv[i]==0) continue;      
        light_cells[i]->label("x");
        light_cells[i]->redraw();
    }

    return;
}


void LightsWindow::help_cbk(fltk::Widget * widget, void*){

    if(win_help == 0){
        win_help = new Window(420,340);
        win_help->begin();
        MultiLineOutput *h_view = new MultiLineOutput(10,0,400,320,0);
        h_view->value("\nNYNO - Lights\n"
        "---------------\n"
        "By: Sean Zhang (sea.sky.heart@gmail.com)\n"
        "At: Aug, 2007\n"
        "\n= Introduction =\n"
        "---------------\n"
        "NYNO-Lights is basically a game to highlight all the cells.\n"
        "For more info please visit [ http://www.noyesno.net/ ]\n"
        
        "\n= How To Play=\n"
        "---------------\n"
        "Each time you click an cell, the cell itself will switch between on and off.\n"
        "The tricky part is the four cells around it will also change the status.\n"
        "You *win* when all the lights are on.\n"
        
        "\n= Credit =\n"
        "---------------\n"
        "" 
        );
        h_view->position(h_view->size(),h_view->size());
        win_help->end();
    }
    win_help->show();
}
